package com.ruoyi.project.wx.service.impl;


import com.ruoyi.project.wx.dao.GoodsGoodsDao;
import com.ruoyi.project.wx.dao.GoodsProductDao;
import com.ruoyi.project.wx.dao.MemberUserDao;
import com.ruoyi.project.wx.domain.GoodsGoodsDO;
import com.ruoyi.project.wx.domain.GoodsProductDO;
import com.ruoyi.project.wx.domain.MemberUserDO;
import com.ruoyi.project.wx.service.GoodsProductService;
import com.ruoyi.project.wx.service.GoodsSpikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class GoodsProductServiceImpl implements GoodsProductService {
	@Autowired
	private GoodsProductDao goodsProductDao;
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private GoodsSpikeService goodsSpikeService;
	
	
	@Override
	public GoodsProductDO get(String id){
		return goodsProductDao.get(id);
	}
	
	@Override
	public List<GoodsProductDO> list(Map<String, Object> map){
		return goodsProductDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsProductDao.count(map);
	}
	
	@Override
	public int save(GoodsProductDO goodsProduct){
		return goodsProductDao.save(goodsProduct);
	}
	
	@Override
	public int update(GoodsProductDO goodsProduct){
		return goodsProductDao.update(goodsProduct);
	}
	
	@Override
	public int remove(String id){
		return goodsProductDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsProductDao.batchRemove(ids);
	}
	
	/**
	 * 生成货号前缀的属性
	 */
	private static final char[] _UU32 = "0123456789abcdefghijklmnopqrstuv".toCharArray();
	
	/**
	 * 生成货号前缀的方法
	 * @param uu
	 * @return
	 */
	public static String UU32(UUID uu) {
		StringBuilder sb = new StringBuilder();
		long m = uu.getMostSignificantBits();
		long l = uu.getLeastSignificantBits();
		int i;
		for(i = 0; i < 13; ++i) {
			sb.append(_UU32[(int)(m >> (13 - i - 1) * 5) & 31]);
		}
		for(i = 0; i < 13; ++i) {
			sb.append(_UU32[(int)(l >> (13 - i - 1) * 5) & 31]);
		}
		return sb.toString();
	}

	@Override
	public String getSkuPrefix() {
		String uuid = "S" + UU32(UUID.randomUUID()).substring(1, 12).toUpperCase();
		List<GoodsProductDO> list = goodsProductDao.queryBySku(uuid);
		if (list!=null && list.size()>0){
			return getSkuPrefix();
		}
		return uuid;
	}

	/*真删除*/
	@Override
	public int removeByGoodsId(String goodsId) {
		return goodsProductDao.removeByGoodsId(goodsId);
	}

	/*假删除*/
	@Override
	public int removeByGoodsIdJ(String goodsId) {
		return goodsProductDao.removeByGoodsIdJ(goodsId);
	}

	@Override
	public GoodsProductDO getByGoodsIdAndSpec(Map<String, Object> params) {
		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
		
		
		String type = params.get("type").toString();
		GoodsProductDO goodsProductDO = new GoodsProductDO();
		if ("0".equals(type)) {
			goodsProductDO =goodsProductDao.getByGoodsIdAndSpec(params);
			//returnPrice(memberUserDO, goodsProductDO);
		} else if ("1".equals(type)) {
			goodsProductDO =goodsProductDao.getBySpikeIdAndSpec(params);
			//returnPrice(memberUserDO, goodsProductDO);
		} else if ("2".equals(type)) {
			goodsProductDO =goodsProductDao.getByNineIdAndSpec(params);
			//returnPrice(memberUserDO, goodsProductDO);
		} else if ("3".equals(type)) {
			goodsProductDO =goodsProductDao.getBargainIdAndSpec(params);
			//returnPrice(memberUserDO, goodsProductDO);
		}
		if (goodsProductDO!=null){
			GoodsGoodsDO goodsGoodsDO =goodsGoodsDao.get(goodsProductDO.getGoodsId());
			if (goodsGoodsDO!=null){
				goodsProductDO.setTitle(goodsGoodsDO.getTitle());
			}
		}
		return goodsProductDO;
	}

	private void returnPrice(MemberUserDO memberUserDO, GoodsProductDO goodsProductDO) {
		if(memberUserDO.getLevel()=='A'){
			goodsProductDO.setPrice(goodsProductDO.getPriceA());
		}else if(memberUserDO.getLevel()=='B'){
			goodsProductDO.setPrice(goodsProductDO.getPriceB());
		}else if (memberUserDO.getLevel()=='C'){
			goodsProductDO.setPrice(goodsProductDO.getPriceC());
		}
	}
	
}
